package com.beecloud.learning.charpter.three;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Created with IntelliJ IDEA.
 * User: hengsun
 * Date: 3/28/17
 * Time: 1:31 PM
 * Description:
 */
public class EventSource extends Thread {
    //private BlockingQueue<EventListener> listeners = new LinkedBlockingDeque<EventListener>();
    private AtomicReference<EventListener> listeners = new AtomicReference<>();

    @Override
    public void run() {
        while (true) {
            EventListener listener = listeners.getAndSet(null);
            if (listener != null)
                listener.onEvent(null);

        }
    }

    public void registerListener(EventListener eventListener) {
        listeners.set(eventListener);
    }
}
